//
//  Kratos_HttpClient.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/7.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_HttpClient.h"
#import "AFNetworking.h"
#import "Kratos_HttpResponseHandle.h"
#import "Kratos_HttpManagerHeader.h"
#import "k_const.h"
#import "Kratos_Options.h"
#import "Kratos_Tool.h"
#import "NSString+Base64.h"
#import "MBProgressHUD+HUD.h"

@interface Kratos_HttpClient () <Kratos_HttpResponseHandle>
@property (nonatomic,weak) id<Kratos_HttpResponseHandle> Kra_responseHandle;
@property (nonatomic,strong) AFHTTPSessionManager *Kra_sessionManager;
@property (nonatomic,strong) NSString *appedURL;
@end


@implementation Kratos_HttpClient


- (instancetype)initWithHandle:(id<Kratos_HttpResponseHandle>)responseHandle
{
    self = [super init];
    if (self)
    {
        _Kra_responseHandle  = responseHandle;
        
        NSMutableString *sss =  [NSMutableString stringWithString:kURL];

        [Kratos_HttpConfigure sharedHttpConfigure].Kra_generalServer =  sss;

        [Kratos_HttpConfigure sharedHttpConfigure].Kra_generalHeaders = @{@"Origin":@"http://game.lzgame.top",@"X-Requested-With":@"XMLHttpRequest"};
        [self Kratos_generalParm:[Kratos_HttpConfigure sharedHttpConfigure].Kra_option];
        
        Kratos_Options  *option = [Kratos_HttpConfigure sharedHttpConfigure].Kra_option;
        
        
        NSString *key = @"isFirstLogin";
        BOOL isFirst = [[NSUserDefaults standardUserDefaults] boolForKey:key];
        if (!isFirst) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [self Kratos_getChid:option];
        }
        
        
        
#ifdef DEBUG
        [Kratos_HttpConfigure sharedHttpConfigure].Kra_enableDebug = YES;
#else
        [Kratos_HttpConfigure sharedHttpConfigure].Kra_enableDebug = false;
#endif

    }
    return self;
}
- (void)Kratos_generalParm:(Kratos_Options *)option
{
    NSString *chid =  [[NSUserDefaults standardUserDefaults]stringForKey:@"chid"];
    NSMutableString *sss =  [NSMutableString string];
    [sss appendFormat:@"gameid=%@&",option.Kratos_GameID];
    [sss appendFormat:@"cid=%@&",option.Kratos_CID];
    [sss appendFormat:@"chid=%@&",chid];
    [sss appendFormat:@"idfa=%@&",option.Kratos_idfa];
    [sss appendFormat:@"sdkver=%@&",option.Kratos_sdkver];
    [sss appendFormat:@"pkg=%@&",option.Kratos_pkg];
    [sss appendFormat:@"version=%@&",option.Kratos_version];
    [sss appendFormat:@"exmodel=%@&",option.Kratos_exmodel];
    [sss appendFormat:@"inm=1"];
    [sss appendFormat:@"rfapp=1"];
    [sss appendFormat:@"time=%@",[Kratos_Tool Kratos_getTimestamp]];
  NSString *base64 =  [sss Kratos_base64EncodedString];
    self.appedURL = base64;
}




- (void)Kratos_onSuccess:(id)responseObject
{
    
}


- (void)Kratos_onFail:(id)clientInfo Kratos_errCode:(NSInteger)errCode errInfo:(NSString *)errInfo
{
    
}

- (void)Kratos_getChid:(Kratos_Options *)option
{
    
    
//    NSString *initCallBackStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"InitCallBack"];
//    if ([initCallBackStr isEqualToString:@"NO"]) return;
   if  ([Kratos_Tool Kratos_getCallback_Kratos] == false)
   {
       return;
   }
    
    NSMutableString *sss =  [NSMutableString stringWithString:@"https://api.lzgame.top"];
    [sss appendFormat:@"/app/repact?"];
    [sss appendFormat:@"inm=1&"];
    [sss appendFormat:@"gameid=%@&",option.Kratos_GameID];
    [sss appendFormat:@"cid=%@&",option.Kratos_CID];
    [sss appendFormat:@"exmodel=%@&",option.Kratos_exmodel];
    [sss appendFormat:@"username=%@&",option.Kratos_idfa];
    [sss appendFormat:@"username=%@&",option.Kratos_idfa];
    [sss appendFormat:@"pkg=%@&",option.Kratos_pkg];
    [sss appendFormat:@"version=%@&",option.Kratos_version];
    [sss appendFormat:@"idfa=%@&",option.Kratos_idfa];
    [sss appendFormat:@"sdkver=%@",option.Kratos_sdkver];
    [sss appendFormat:@"rfapp=1"];
    
    
    AFHTTPSessionManager *session =  [AFHTTPSessionManager  manager];
    AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
    [requestSerializer setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    [requestSerializer setValue:@"http://game.lzgame.top" forHTTPHeaderField:@"Origin"];
    session.requestSerializer = requestSerializer;
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [session POST:sss parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            Kratos_HttpConfigure.sharedHttpConfigure.Kra_option.Kratos_chidID = [responseObject objectForKey:@"chid"];
            [[NSUserDefaults standardUserDefaults] setObject:[responseObject objectForKey:@"chid"] forKey:@"chid"];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        kLog(@"failure%@",error);
    }];
    
    
    
}


- (NSString *)Kratos_post:(NSString *)URLString Kratos_parameters:(id)parameters Kratos_onSuccess:( Kratos_HttpResponseBlock )responseresult Kratos_onFail:(Kratos_HttpResponseBlock )fail;
{
    __weak typeof(self) weakSelf = self;
    
    UIViewController *vc = [Kratos_Tool Kratos_currentViewController];
    
    
    [MBProgressHUD showActivityMessage:@"加载中" view:vc.view];
    NSString *requestId = [[Kratos_HttpManager sharedHttpManager] Kratos_sendRequestWithConfigBlock:^(Kratos_HttpRequst * _Nullable request) {
        
        request.Kra_requestURL = [URLString stringByAppendingFormat:@"?base64code=%@",self.appedURL];
        request.Kra_requestMethod = Kratos_HttpRequestTypePost;
        request.Kra_normalParams = parameters;
        
        
    } Kratos_complete:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status ==Kratos_HttpResponseStatusSuccess)
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onSuccess:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onSuccess:response];
            }
            [MBProgressHUD hideHUDForView:vc.view];

            responseresult(response);
        }else
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onFail:Kratos_errCode:errInfo:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onFail:parameters  Kratos_errCode:response.Kra_statueCode errInfo:response.Kra_content];
            }
            [MBProgressHUD hideHUDForView:vc.view];

            fail(response);
        }
        
        
    }];
    return requestId;
}

- (NSString *)Kratos_get:(NSString *)URLString Kratos_parameters:(id)parameters Kratos_onSuccess:( Kratos_HttpResponseBlock )responseresult Kratos_onFail:(Kratos_HttpResponseBlock )fail
{
    
   
    __weak typeof(self) weakSelf = self;
    NSString *requestId = [[Kratos_HttpManager sharedHttpManager] Kratos_sendRequestWithConfigBlock:^(Kratos_HttpRequst * _Nullable request) {
        request.Kra_requestURL =  [URLString stringByAppendingString:self.appedURL];;
        request.Kra_requestMethod = Kratos_HttpRequestTypeGet;
        request.Kra_normalParams = parameters;
    } Kratos_complete:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status ==Kratos_HttpResponseStatusSuccess)
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onSuccess:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onSuccess:response];
            }
            responseresult(response);

        }else
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onFail:Kratos_errCode:errInfo:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onFail:parameters  Kratos_errCode:response.Kra_statueCode errInfo:response.Kra_content];
            }
            fail(response);

        }
        
    }];
    return requestId;
}

- (NSString *)Kratos_noGeneralget:(NSString *)URLString Kratos_parameters:(id)parameters onSuccess:( Kratos_HttpResponseBlock )responseresult onFail:(Kratos_HttpResponseBlock )fail
{
    
    __weak typeof(self) weakSelf = self;
    NSString *requestId = [[Kratos_HttpManager sharedHttpManager] Kratos_sendRequestWithConfigBlock:^(Kratos_HttpRequst * _Nullable request) {
        request.Kra_requestURL = URLString;
        request.Kra_requestMethod = Kratos_HttpRequestTypeGet;
        request.Kra_normalParams = parameters;
    } Kratos_complete:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status ==Kratos_HttpResponseStatusSuccess)
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onSuccess:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onSuccess:response];
            }
            responseresult(response);
            
        }else
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onFail:Kratos_errCode:errInfo:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onFail:parameters  Kratos_errCode:response.Kra_statueCode errInfo:response.Kra_content];
            }
            fail(response);
            
        }
        
    }];
    return requestId;
}
- (NSString *)Kratos_noGeneralpost:(NSString *)URLString Kratos_parameters:(id)parameters onSuccess:( Kratos_HttpResponseBlock )responseresult onFail:(Kratos_HttpResponseBlock )fail
{
    __weak typeof(self) weakSelf = self;
    NSString *requestId = [[Kratos_HttpManager sharedHttpManager] Kratos_sendRequestWithConfigBlock:^(Kratos_HttpRequst * _Nullable request) {
        request.Kra_requestURL = URLString;
        request.Kra_requestMethod = Kratos_HttpRequestTypeGet;
        request.Kra_normalParams = parameters;
    } Kratos_complete:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status ==Kratos_HttpResponseStatusSuccess)
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onSuccess:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onSuccess:response];
            }
            responseresult(response);
            
        }else
        {
            if ([weakSelf.Kra_responseHandle respondsToSelector:@selector(Kratos_onFail:Kratos_errCode:errInfo:)])
            {
                [weakSelf.Kra_responseHandle Kratos_onFail:parameters  Kratos_errCode:response.Kra_statueCode errInfo:response.Kra_content];
            }
            fail(response);
            
        }
        
    }];
    return requestId;
}

@end
